package com.awenhui.demo.config.shiro;

import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.DefaultHashService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.crypto.hash.format.HexFormat;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 注册时需要生成密码和密码盐存入数据库
 *
 * @author yuxf
 * @version 1.0
 * @date 2020/12/22 17:01
 */
public class PasswordHelper {
    private static String algorithmName = "md5";
    private static final int hashIterations = 2;

    /**
     * 获取随机密码盐
     *
     * @return
     */
    public static String getSalt() {
        RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
        String salt = randomNumberGenerator.nextBytes().toHex();
        return salt;
    }

    /**
     * 生成密码
     *
     * @param plainPassword 明文密码
     * @param salt          密码盐
     * @return
     */
    public static String getPassowrd(String plainPassword, String salt) {
        String newPassword = new SimpleHash(algorithmName, plainPassword, salt, hashIterations).toHex();
        return newPassword;
    }



}
